package com.simple.car.loader.server.tools;

import android.annotation.SuppressLint;
import android.content.Context;
import android.location.Address;
import android.location.Geocoder;
import android.location.LocationListener;
import android.location.LocationManager;
import android.util.Log;

import com.simple.car.loader.server.annotation.Function;
import com.simple.car.loader.server.entity.RunningInfo;
import com.simple.car.loader.server.entity.vo.RunningInfoVo;

import java.util.List;
import java.util.Locale;

@Function
public class LocalTool {

    private String serviceName = Context.LOCATION_SERVICE;

    private RunningInfo runningInfo = new RunningInfo();

    private Object object = new Object();

    private Context context;

    private LocationListener locationListener;

    public RunningInfoVo getRunningVo() {
        RunningInfoVo runningInfoVo = new RunningInfoVo();
        synchronized (object) {
            runningInfoVo.setLon(runningInfo.getLon());
            runningInfoVo.setLat(runningInfo.getLat());
            runningInfoVo.setHeight(runningInfo.getHeight());
            runningInfoVo.setSpeed(runningInfo.getSpeed());
            runningInfoVo.setPosition(runningInfo.getPosition());
        }
        return runningInfoVo;
    }

    @SuppressLint("MissingPermission")
    public void register(Context context) {
        this.context = context;
        LocationManager locationManager = (LocationManager) context.getSystemService(serviceName);
        locationListener = location -> {
            synchronized (object) {
                runningInfo.setLon(location.getLongitude());
                runningInfo.setLat(location.getLatitude());
                runningInfo.setHeight(location.getAltitude());
                runningInfo.setSpeed(location.getSpeedAccuracyMetersPerSecond() * 3600 / 1000.0);
                runningInfo.setPosition(getAddress(context));
            }
        };
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 20, locationListener);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 20, locationListener);
    }

    public void unregister(Context context) {
        if (locationListener == null) {
            return;
        }
        LocationManager locationManager = (LocationManager) context.getSystemService(serviceName);
        locationManager.removeUpdates(locationListener);
    }

    //获取地址信息:城市、街道等信息
    private String getAddress(Context context) {
        List<Address> result;
        try {
            if (runningInfo != null) {
                Geocoder gc = new Geocoder(context, Locale.getDefault());
                result = gc.getFromLocation(runningInfo.getLat(), runningInfo.getLon(), 1);
                if (result.size() <= 0) {
                    return null;
                }
                Address address = result.get(0);
                return address.getAddressLine(0);
            } else {
                return null;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public Context getContext() {
        return context;
    }
}
